package com.sls.web.manager.action.system;

import javax.annotation.Resource;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.sys.pojo.Role;
import com.sls.web.manager.sys.querybean.RoleInfoQuery;
import com.sls.web.manager.sys.service.RoleService;

public class RoleAction extends SuperAction {
	private static final long serialVersionUID = -5400615207972560221L;
	@Resource(name = "roleService")
	private RoleService roleServie;
	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private RoleInfoQuery roleQuery;
	private Role role;
	private String id;

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public RoleInfoQuery getRoleQuery() {
		return roleQuery;
	}

	public void setRoleQuery(RoleInfoQuery roleQuery) {
		this.roleQuery = roleQuery;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	// 列表
	public String list() {
		if (roleQuery == null) {
			roleQuery = new RoleInfoQuery();
			roleQuery.setPageSize(Constants.INIT_PAGESIZE);
			roleQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = roleServie.queryRole(roleQuery, getCurrentOperator());
		} catch (Exception e) {
			addActionError(e.getMessage());
		}

		return "list";
	}

	// 添加
	public String add() {
		return "edit";
	}

	// 添加角色
	public String addRole() throws Exception {
		boolean result = roleServie.addRole(role, this.getCurrentOperator());
		if (!result) {
			addActionError("添加失败!");
			return this.add();
		} else {
			redirectionUrl = "role!list.htm";
			return "success";
		}
	}

	// 编辑
	public String edit() throws Exception {
		if (id != null) {
			role = roleServie.queryRoleInfoById(id, this.getCurrentOperator());
		}
		return "edit";
	}

	// 编辑角色
	public String editRole() throws Exception {
		try {
			roleServie.updateRole(role, this.getCurrentOperator());
			redirectionUrl = "role!list.htm";
			return "success";
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return this.edit();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.edit();
		}
	}

	// 删除
	public String delete() throws Exception {
		try{
			roleServie.delete(id, getCurrentOperator());
			redirectionUrl = "role!list.htm";
			return "success";
		}catch (ManagerException e){
			addActionError(e.getMessage());
			return this.list();
		}
	}
}